﻿using Abp.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;
using System.Data.Common;

namespace HCD.EntityFrameworkCore
{
    public static class DbContextOptionsConfigurer
    {
        #region DefineLoggerFactory     
        public static readonly ILoggerFactory EFLoggerFactory = LoggerFactory.Create(builder => { builder.AddDebug(); });
        #endregion DefineLoggerFactory
        public static void Configure<T>(
            DbContextOptionsBuilder<T> dbContextOptions,
            string connectionString) where T : AbpDbContext
        {
#if DEBUG
            dbContextOptions.UseLoggerFactory(EFLoggerFactory);
            dbContextOptions.EnableSensitiveDataLogging();
            dbContextOptions.EnableDetailedErrors();
#endif
            /* This is the single point to configure DbContextOptions for EnterpriseDbContext */
            if(typeof(T) == typeof(HisDbContext))
                dbContextOptions.UseOracle(connectionString,options => options.UseOracleSQLCompatibility("11"));
            else
                dbContextOptions.UseMySql(connectionString);
        }

        public static void Configure<T>(
            DbContextOptionsBuilder<T> dbContextOptions,
            DbConnection connection) where T : AbpDbContext
        {
#if DEBUG
            dbContextOptions.UseLoggerFactory(EFLoggerFactory);
            dbContextOptions.EnableSensitiveDataLogging();
            dbContextOptions.EnableDetailedErrors();
#endif
            /* This is the single point to configure DbContextOptions for EnterpriseDbContext */
            if(typeof(T) == typeof(HisDbContext))
                dbContextOptions.UseOracle(connection,options => options.UseOracleSQLCompatibility("11"));
            else
                dbContextOptions.UseMySql(connection);
        }
    }
}
